package com.squid.dataCenter.service.datadictionary;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.squid.base.common.bean.PageReq;
import com.squid.base.common.bean.PageResp;
import com.squid.base.common.enums.CommonErrorEnums;
import com.squid.base.common.enums.DeleteTokenEnums;
import com.squid.base.common.error.ExceptionFactory;
import com.squid.base.common.mapper.EasyServiceImpl;
import com.squid.base.utils.LimitUtils;
import com.squid.dataCenter.convertor.DataDictionaryEntityConvertor;
import com.squid.dataCenter.model.entity.DataDictionaryEntity;
import com.squid.dataCenter.model.mapper.DataDictionaryEntityMapper;
import com.squid.dataCenter.model.req.datadictionary.DataDictionaryInsertReq;
import com.squid.dataCenter.model.req.datadictionary.DataDictionaryPageReq;
import com.squid.dataCenter.model.req.datadictionary.DataDictionaryUpdateReq;
import com.squid.dataCenter.model.resp.datadictionary.DataDictionaryPageResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 数据字典服务
 *
 * @author jinhang1
 * @date 2024/06/27
 */
@Service
public class DataDictionaryServiceImpl extends EasyServiceImpl<DataDictionaryEntityMapper, DataDictionaryEntity>
        implements DataDictionaryService {
    @Resource
    DataDictionaryEntityMapper dataDictionaryEntityMapper;

    @Override
    public PageResp<DataDictionaryPageResp> paging(PageReq<DataDictionaryPageReq> req) {
        Page<DataDictionaryPageResp> page = new Page<>(req.getPageNum(), req.getPageSize());
        try {
            IPage<DataDictionaryPageResp> response = dataDictionaryEntityMapper.page(page, req.getRequest());
            return LimitUtils.formatResponse(response);
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException(CommonErrorEnums.CODE_1003, e);
        }
    }

    private void repeat(String code, String parentCode, Long id) {
        DataDictionaryEntity dataDictionaryEntity = this.getOne(new LambdaQueryWrapper<DataDictionaryEntity>()
                .eq(DataDictionaryEntity::getParentCode, parentCode)
                .eq(DataDictionaryEntity::getCode, code)
                .eq(DataDictionaryEntity::getDeleteToken, DeleteTokenEnums.N.getCode())
                .ne(!ObjectUtils.isEmpty(id), DataDictionaryEntity::getId, id)
        );
        if (!ObjectUtils.isEmpty(dataDictionaryEntity)) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1001);
        }
    }

    @Override
    public void add(DataDictionaryInsertReq req) {
        DataDictionaryEntity dataDictionaryEntity = DataDictionaryEntityConvertor.INSTANCE.insertTo(req);
        repeat(req.getCode(), req.getParentCode(), null);
        try {
            this.save(dataDictionaryEntity);
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException(CommonErrorEnums.CODE_1005, e);
        }
    }

    @Override
    public void update(DataDictionaryUpdateReq req) {
        DataDictionaryEntity dataDictionaryEntity = DataDictionaryEntityConvertor.INSTANCE.updateTo(req);
        repeat(req.getCode(), req.getParentCode(), req.getId());
        try {
            this.updateById(dataDictionaryEntity);
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException(CommonErrorEnums.CODE_1002, e);
        }
    }

    @Override
    public void delete(Long id) {
        try {
            this.removeById(id);
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException(CommonErrorEnums.CODE_1004, e);
        }
    }
}
